﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using OptimumInfra.ViewData;

namespace OptimumInfra.Models
{
    public class OrderRepository
    {
        private OptimumDataContext op = new OptimumDataContext();
        #region  orders
        public List<ViewDataOrder> GetAllOrders()
        {
            var result = from r in op.Orders
                         join c in op.Customers on r.CustomerId equals c.Id
                         select new { r.Id, r.DateAdd,r.CustomerId, r.State, r.DateUpdate,  c.LastName, c.FirstName };

            List<ViewDataOrder> t = new List<ViewDataOrder>();
            foreach (var r in result)
            {
                ViewDataOrder temp = new ViewDataOrder();
                temp.Id = r.Id;
                temp.FirstName = r.FirstName;
                temp.Status = OrderStatus(r.State);
                t.Add(temp);
            }
            return t;
        }

        public string OrderStatus(int status)
        {
            string result = "Unknown";
            if (status == 1)
                result = "In progress";
            else if (status == 2)
                result = "Paid";
            return result;
        }

        public Order CreateOrder(int CusomterId)
        {
            Order o = new Order();
            o.CustomerId = CusomterId;
            o.DateAdd =  DateTime.Now;
            o.State = 1; //in progess
            op.Orders.InsertOnSubmit(o);
            op.SubmitChanges();
            return o;
        }

        public ViewDataOrder GeOrderById(int id)
        {
            var result = from r in op.Orders
                         join c in op.Customers on r.CustomerId equals c.Id where r.Id == id

                         select new { r.Id, r.DateAdd, r.CustomerId, r.DateUpdate,  r.State, c.LastName, c.FirstName };

            ViewDataOrder order= new ViewDataOrder();
            order.OrderProduct = new List<OrderProduct>();
            
            foreach (var r in result)
            {  
                order.Id = r.Id;
                order.FirstName = r.FirstName;
                order.Status = OrderStatus(r.State);
                order.DateAdd = (DateTime)r.DateAdd;
                order.DateUpdate = (DateTime) r.DateUpdate;
                var oProduct = (from oPr in op.OrderProducts
                               where oPr.OrderId == r.Id
                               select oPr).ToList();
                order.OrderProduct = oProduct;     
            }
            return order;

        }

        public void DeleteResourceById(Resource r)
        {

            op.Resources.DeleteOnSubmit(r);
            op.SubmitChanges();
        }

        public List<ViewDataOrder> GetOrdersByCustomer(int customerId)
        {
            var result = from r in op.Orders
                         join c in op.Customers on r.CustomerId equals c.Id
                         where r.CustomerId == customerId
                         select new { r.Id, r.DateAdd, r.State, r.CustomerId, r.DateUpdate, c.LastName, c.FirstName };

            List<ViewDataOrder> t = new List<ViewDataOrder>();
            foreach (var r in result)
            {
                ViewDataOrder temp = new ViewDataOrder();
                temp.Id = r.Id;
                temp.FirstName = r.FirstName;
                temp.Status = OrderStatus(r.State);
                t.Add(temp);
            }
            return t;

        }

        #endregion
    }
}